#python -u "d:\nyahentai\DoujinshiInfotoMarkdown.py"
import os

def getCheckBracket(info):
    if "[" not in info:
        return [False,False]
    if info.count("[") == 1:
        if info[len(info)-1] == "]":
            return [False,True]
        else:
            return [True,False]
    else:
        return [True,True]

def getType(bracket0,bracket1,info):
    if bracket0 == bracket1:
        if bracket0 == True:#tt
            if "(" in info[info.find("["):info.find("]")]:
                info = info[:info.find("(")]+"（"+info[info.find("(")+1:info.find(")")]+"）"+info[info.find(")")+1:]
            if "(" not in info:
                return 232
            elif info.count("(") == 1:
                if info[0] == "(":
                    return 1232
                else:
                    return 2312
            else:
                return 12312
        else:#ff
            if "(" not in info:
                return 3
            elif info.count("(") == 1:
                if info[0] == "(":
                    return 13
                else:
                    return 31
            else:
                return 131
 
    if bracket0 != bracket1:
        if bracket0 == True:#tf
            if "(" in info[info.find("["):info.find("]")]:
                info = info[:info.find("(")]+"（"+info[info.find("(")+1:info.find(")")]+"）"+info[info.find(")")+1:]
            if "(" not in info:
                return 23
            elif info.count("(") == 1:
                if info[0] == "(":
                    return 123
                else:
                    return 231
            else:
                return 1231
        else:#ft
            if "(" not in info:
                return 32
            elif info.count("(") == 1:
                if info[0] == "(":
                    return 132
                else:
                    return 312
            else:
                return 1312

    '''
    tt
    info "()[]doujinshi()[]"12doujinshi12
    info = "()[]doujinshi[]"12doujinshi2
    info = "[]doujinshi()[]"2doujinshi12
    info = "[]doujinshi[]"2doujinshi2
    ff
    info = "()doujinshi()"11
    info = "()doujinshi"1doujinshi0
    info = "doujinshi()"0doujinshi1
    info = "doujinshi"0
    '''
    '''
    tf
    info = "()[]doujinshi()"12doujinshi1
    info = "()[]doujinshi"12doujinshi0
    info = "[]doujinshi()"2doujinshi1
    info = "[]doujinshi"2doujinshi0
    ft
    info = "()doujinshi()[]"1doujinshi12
    info = "()doujinshi[]"1doujinshi2
    info = "doujinshi()[]"0doujinshi12
    info = "doujinshi[]"0doujinshi2
    '''

def getInfo(type,info):
    if type ==3:
        return [info,"",""]
    elif type ==32:
        return [info[:info.rfind("[")],"",""]
    elif type ==31:
        return [info[:info.rfind("(")],"",info[info.rfind("(")+1:info.rfind(")")]]
    elif type ==312:
        return [info[:info.rfind("(")],"",info[info.rfind("(")+1:info.rfind(")")]]
    elif type ==23:
        if "(" in info[info.find("["):info.find("]")]:
            info = info[:info.find("(")]+"（"+info[info.find("(")+1:info.find(")")]+"）"+info[info.find(")")+1:]
        return [info[info.find("]")+1:],info[info.find("[")+1:info.find("]")],""]
    elif type ==232:
        if "(" in info[info.find("["):info.find("]")]:
            info = info[:info.find("(")]+"（"+info[info.find("(")+1:info.find(")")]+"）"+info[info.find(")")+1:]
        return [info[info.find("]")+1:info.rfind("[")],info[info.find("[")+1:info.find("]")],""]
    elif type ==231:
        if "(" in info[info.find("["):info.find("]")]:
            info = info[:info.find("(")]+"（"+info[info.find("(")+1:info.find(")")]+"）"+info[info.find(")")+1:]
        return [info[info.find("]")+1:info.rfind("(")],info[info.find("[")+1:info.find("]")],info[info.rfind("(")+1:info.rfind(")")]]
    elif type ==2312:
        if "(" in info[info.find("["):info.find("]")]:
            info = info[:info.find("(")]+"（"+info[info.find("(")+1:info.find(")")]+"）"+info[info.find(")")+1:]
        return [info[info.find("]")+1:info.rfind("(")],info[info.find("[")+1:info.find("]")],info[info.rfind("(")+1:info.rfind(")")]]
    elif type ==13:
        return [info[info.find(")")+1:],"",""]
    elif type ==132:
        return [info[info.find(")")+1:info.rfind("[")],"",""]
    elif type ==131:
        return [info[info.find(")")+1:info.rfind("(")],"",info[info.rfind("(")+1:info.rfind(")")]]
    elif type ==1312:
        return [info[info.find(")")+1:info.rfind("(")],"",info[info.rfind("(")+1:info.rfind(")")]]
    elif type ==123:
        if "(" in info[info.find("["):info.find("]")]:
            info = info[:info.find("(")]+"（"+info[info.find("(")+1:info.find(")")]+"）"+info[info.find(")")+1:]
        return [info[info.find(")")+1:],info[info.find("[")+1:info.find("]")],""]
    elif type ==1232:
        if "(" in info[info.find("["):info.find("]")]:
            info = info[:info.find("(")]+"（"+info[info.find("(")+1:info.find(")")]+"）"+info[info.find(")")+1:]
        return [info[info.find("]")+1:info.rfind("[")],info[info.find("[")+1:info.find("]")],""]
    elif type ==1231:
        if "(" in info[info.find("["):info.find("]")]:
            info = info[:info.find("(")]+"（"+info[info.find("(")+1:info.find(")")]+"）"+info[info.find(")")+1:]
        return [info[info.find("]")+1:info.rfind("(")],info[info.find("[")+1:info.find("]")],info[info.rfind("(")+1:info.rfind(")")]]
    else:
        if "(" in info[info.find("["):info.find("]")]:
            info = info[:info.find("(")]+"（"+info[info.find("(")+1:info.find(")")]+"）"+info[info.find(")")+1:]
        return [info[info.find("]")+1:info.rfind("(")],info[info.find("[")+1:info.find("]")],info[info.rfind("(")+1:info.rfind(")")]]
count = 1
while  count < 16:
    print("D:\\nyahentai\\females-only\\"+str(count * 20)+".md")
    f1 = open("D:\\nyahentai\\females-only\\"+str(count * 20)+".md","w",encoding="utf-8")
    f1.close()
    FileList = os.listdir("D:\\nyahentai\\females-only\\"+str(count * 20)+"\\")
    for file in FileList:
        f2 = open("D:\\nyahentai\\females-only\\"+str(count * 20)+"\\"+file,"r",encoding="utf-8")
        LineList = f2.readlines()
        f2.close()
        f1 = open("D:\\nyahentai\\females-only\\"+str(count * 20)+".md","a",encoding="utf-8")
        f1.write("\n"+file+"\n")
        f1.write("|link|doujinshi|author|parody|thumb|\n")
        f1.write("| -------- | ------- | ------ | ------ | ------ |\n")
        for line in LineList:
            if "/g/" in line:
                str1 = line[line.find("/"):line.find("\" class")]
                url = "https://zhb.doghentai.com"+str1
                f1.write("|[link]("+url+")|")
                continue
            elif "data-src" in line:
                info = line[line.find("alt=\"")+5:line.rfind("\"")]
                if info == "":
                    f1.write(info+"| | |")
                else:
                    Bracket = getCheckBracket(info)
                    Type = getType(Bracket[0],Bracket[1],info)
                    for text in getInfo(Type,info):
                        f1.write(text+"|")
                ThumbUrl = line[line.find("data-src=\"")+10:line.find(".jpg")+4]
                f1.write("![thumb.jpg]("+ThumbUrl+")|\n")
                continue
        f1.close()
    #exit()
    count += 1